define([ "app", "jquery", "md5", "ecb", 
         "directives/DatetimePicker",
         "services/UserService",
         "resources/OrgResource",
		 "resources/UserResource",
		 "resources/ConferenceDetailResource",
		 "services/MessageBox",
		 "directives/common/OrgUserPickerConference",
		 "directives/common/OrgTreeView",
		 "directives/authority/EditCauthority",
		 "services/TipsService"
		],
		function(app) {

	var deps = [ "$scope", "$state", "$window",  "$location",
			"$stateParams", "MenuService", "UserService","OrgResource", "UserResource","ConferenceDetailResource","MessageBox","TipsService"];

	function controller($scope, $state, $window, $location, $stateParams, MenuService, UserService,OrgResource, UserResource,ConferenceDetailResource,MessageBox,TipsService) {
		// 通过go传递参数-主键ID 
		var keyId=$stateParams.keyId;
		$scope.user ={};
		$scope.user = UserService.getCurrentUser();
		if(keyId==null || keyId==""){
			keyId =$scope.user.currentCompanyCode;
		}
		$scope.companyCode=keyId;
		$scope.currentPage = 1;
        $scope.pages = [];
        $scope.pageSize = 10;
		
        $scope.selectedUser = {}; // 点击后保存的单个用户
		$scope.selectedUserList = {}; // 点击后保存的用户列表
		
		queryData();
		
		// 删除权限
		$scope.deleteUser =function (userId){
			var userIds =[];
			userIds.push(userId);
			ConferenceDetailResource.deleteUserAuthority(userIds,'confAuthority',keyId).success(function(data){
				// 置空
				$scope.selectedUserList = {};
        		var searchKey =$scope.searchKey;
        		queryData(searchKey);
        		$scope.isSelectAll = false;
        		
            });
			
		};
		
		// 修改权限,初始化页面
		$scope.updateUserConfig =function (userId){
			ConferenceDetailResource.updateUserConfig(userId,keyId).success(function(data){
				//
	        });
		};
		
		// 选择所有用户
		$scope.toggleAll = function() {
			if ($scope.isSelectAll) {
				$scope.selectedUser = {}; // 点击后保存的单个用户
				$scope.selectedUserList = {}; // 点击后保存的用户列表
				$scope.isSelectAll = false;
			} else {
				for (var i = 0; i < $scope.data.length; i++) {
					$scope.selectedUserList[$scope.data[i].userId] = $scope.data[i];
				}
				$scope.isSelectAll = true;
			}
		};
		
		// 选择一个用户
		$scope.toggleUser = function(record) {
			$scope.selectedUser = record;
			if($scope.selectedUserList[record.userId]) {
				delete $scope.selectedUserList[record.userId];
			} else {
				$scope.selectedUserList[record.userId] = record;
			}
		};
		
		// 删除选择权限-批量删除
		$scope.deleteAuthority =function (){
			var userIds=convertMapToArray($scope.selectedUserList)
			if(userIds.length>0){
				ConferenceDetailResource.deleteUserAuthority(userIds,'confAuthority',keyId).success(function(data){
					// 置空
					$scope.selectedUserList = {};
	        		var searchKey =$scope.searchKey;
	        		$scope.currentPage = 1;
	        		queryData(searchKey);
	        		$scope.isSelectAll = false;
	            });
			}
			
		}
		
		/**
    	 * 对象转列表 .
    	 */
    	function convertMapToArray(map) {
    		var array = [];
    		for(var i in map) {
    			array.push(map[i].userId);
    		}
    		return array;
    	}
		/**
         * 监听刷新广播
         */
        
        $scope.$on('cAuthorityEditBroadCast', function($event,data) {
        	var searchKey =$scope.searchKey;
        	queryData(searchKey);
			$event.preventDefault();
        });
        
		$scope.gotoPage = function (currentNum) {
			if($scope.currentPage != currentNum){
				var searchKey =$scope.searchKey;
				 $scope.currentPage = currentNum;
		            queryData(searchKey);
			}
        };
        
        $scope.nextPage = function () {
        	if($scope.currentPage<$scope.totalPage){
        		var searchKey =$scope.searchKey;
        		$scope.currentPage++;
        		queryData(searchKey);
        	}
        };

        $scope.upPage = function () {
        	if($scope.currentPage>1){
        		var searchKey =$scope.searchKey;
        		$scope.currentPage--;
        		queryData(searchKey);
        	}	
        };
        
        
		$scope.$on('confAuthoryAddBroadCastF', function($event, data) {
			var searchKey =$scope.searchKey;
			queryData(searchKey);
        	$event.preventDefault();
        });
		
		function queryData(searchKey){
			ConferenceDetailResource.queryConferenceAuthorityUserList(keyId,$scope.currentPage,$scope.pageSize,searchKey).success(function(data){
				$scope.data = data.userList;
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
	        });
		}
		
		$scope.search =function (){
			var searchKey =$scope.searchKey;
			$scope.currentPage = 1;
        	queryData(searchKey);
		}
		
        $scope.info = function(data){
        	var userIds=[] ;
        	angular.forEach(data.list, function(item) {
        		userIds.push(item.userId);
        	});
        	if(userIds.length == 0){
        		return;
        	}
        	ConferenceDetailResource.grantConferenceAuthority(userIds, 0,keyId,data.chooseType,data.timeChoose).success(function(operatorFlag){
        		TipsService.show("<span class='glyphicon glyphicon-ok'></span>&nbsp;保存成功", 500);
        		//$scope.tableParams.reload();
        		var searchKey =$scope.searchKey;
        		queryData(searchKey);
        	});
        };
    };
	
	controller.$inject = deps;
	return app.lazy.controller("ConferenceDetailController", controller);
});
